#include <boost/filesystem.hpp>

#include "scene/skybox.h"

using boost::filesystem::path;

namespace engine {
		
	namespace scene {
			
		Skybox::Skybox():
		Entity("sky") {}
		
		Skybox::~Skybox() {
			
		}
		
		bool Skybox::loadSkybox(const string& filepath) {
			string top = (path(filepath) / "top.tga").string();
			string bottom = (path(filepath) / "bottom.tga").string();
			string left = (path(filepath) / "left.tga").string();
			string right = (path(filepath) / "right.tga").string();
			string front = (path(filepath) / "front.tga").string();
			string back = (path(filepath) / "back.tga").string();
			
			if (!getTextureManager()->loadResourceFromFile("SKY_TOP", top)) {
				return false;
			}

			if (!getTextureManager()->loadResourceFromFile("SKY_BOTTOM", bottom)) {
				return false;
			}
	
			if (!getTextureManager()->loadResourceFromFile("SKY_LEFT", left)) {
				return false;
			} 
			
			if (!getTextureManager()->loadResourceFromFile("SKY_RIGHT", right)) {
				return false;
			}
			
			if (!getTextureManager()->loadResourceFromFile("SKY_FRONT", front)) {
				return false;
			}
			
			if (!getTextureManager()->loadResourceFromFile("SKY_BACK", back)) {
				return false;
			}
			
			return true;
		}
		
		void Skybox::render() {
			glDisable(GL_DEPTH_TEST);
			//glDepthMask(GL_FALSE);
			glColor3f(1, 1, 1);
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
			glEnable(GL_TEXTURE_2D);
			
			glBindTexture(GL_TEXTURE_2D, getTextureManager()->getResourceID("SKY_FRONT"));  // Bind the Texture to the object
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glBegin(GL_QUADS);
			glTexCoord2f(0.0, 0.0); glVertex3f(-SKYBOX_SIZE, -SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(1.0, 0.0); glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(1.0, 1.0); glVertex3f( SKYBOX_SIZE,  SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(0.0, 1.0); glVertex3f(-SKYBOX_SIZE,  SKYBOX_SIZE,  SKYBOX_SIZE);
			glEnd();
			
			glBindTexture(GL_TEXTURE_2D, getTextureManager()->getResourceID("SKY_BACK"));
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glBegin(GL_QUADS);
			glNormal3f( 0.0, 0.0,-1.0);
			glTexCoord2f(1.0, 0.0); glVertex3f(-SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(1.0, 1.0); glVertex3f(-SKYBOX_SIZE,  SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.0, 1.0); glVertex3f( SKYBOX_SIZE,  SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.0, 0.0); glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glEnd();

			glBindTexture(GL_TEXTURE_2D, getTextureManager()->getResourceID("SKY_TOP"));
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glBegin(GL_QUADS);
			glNormal3f( 0.0, 1.0, 0.0);
			glTexCoord2f(0.0, 1.0); glVertex3f(-SKYBOX_SIZE,  SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.0, 0.0); glVertex3f(-SKYBOX_SIZE,  SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(1.0, 0.0); glVertex3f( SKYBOX_SIZE,  SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(1.0, 1.0); glVertex3f( SKYBOX_SIZE,  SKYBOX_SIZE, -SKYBOX_SIZE);
			glEnd();

			glBindTexture(GL_TEXTURE_2D, getTextureManager()->getResourceID("SKY_BOTTOM"));
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glBegin(GL_QUADS);
			glNormal3f( 0.0,-1.0, 0.0);
			glTexCoord2f(0.0, 0.0); glVertex3f(-SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(1.0, 0.0); glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(1.0, 1.0); glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(0.0, 1.0); glVertex3f(-SKYBOX_SIZE, -SKYBOX_SIZE,  SKYBOX_SIZE);
			glEnd();

			glBindTexture(GL_TEXTURE_2D, getTextureManager()->getResourceID("SKY_LEFT"));
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glBegin(GL_QUADS);
			glNormal3f( 1.0, 0.0, 0.0);
			glTexCoord2f(1.0, 0.0); glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(1.0, 1.0); glVertex3f( SKYBOX_SIZE,  SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(0.0, 1.0); glVertex3f( SKYBOX_SIZE,  SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(0.0, 0.0); glVertex3f( SKYBOX_SIZE, -SKYBOX_SIZE,  SKYBOX_SIZE);
			glEnd();

			glBindTexture(GL_TEXTURE_2D, getTextureManager()->getResourceID("SKY_RIGHT"));
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glBegin(GL_QUADS);
			glNormal3f(-1.0, 0.0, 0.0);
			glTexCoord2f(0.0, 0.0); glVertex3f(-SKYBOX_SIZE, -SKYBOX_SIZE, -SKYBOX_SIZE);
			glTexCoord2f(1.0, 0.0); glVertex3f(-SKYBOX_SIZE, -SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(1.0, 1.0); glVertex3f(-SKYBOX_SIZE,  SKYBOX_SIZE,  SKYBOX_SIZE);
			glTexCoord2f(0.0, 1.0); glVertex3f(-SKYBOX_SIZE,  SKYBOX_SIZE, -SKYBOX_SIZE);

			glEnd();
			
			glEnable(GL_DEPTH_TEST);
			//glDepthMask(GL_TRUE);

		}
	}
}
